package com.atguigu.study.base.interactive;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ShareData {

    private int flag = 1;//标志
    private Lock lock = new ReentrantLock();//锁
    private Condition condition1 = lock.newCondition();//钥匙1
    private Condition condition2 = lock.newCondition();//钥匙2
    private Condition condition3 = lock.newCondition();//钥匙3

    public void print5() {
        //1.判断
        lock.lock();
        try {
            while (flag != 1) {
                try {
                    condition1.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            //2.干活
            for (int i = 1; i <=5; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //3.通知
            flag=2;
            condition2.signal();
        } finally {

            lock.unlock();

        }
    }
    public void print10() {
        //1.判断
        lock.lock();
        try {
            while (flag != 2) {
                try {
                    condition2.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            //2.干活
            for (int i = 1; i <=10; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //3.通知
            flag=3;
            condition3.signal();
        } finally {

            lock.unlock();

        }
    }
    public void print15() {
        //1.判断
        lock.lock();
        try {
            while (flag != 3) {
                try {
                    condition3.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            //2.干活
            for (int i = 1; i <=15; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //3.通知
            flag=1;
            condition1.signal();
        } finally {
            lock.unlock();
        }
    }

}

/**
 * @Author 快点干早点散
 * @Create 2025/4/23 19:28
 * Description:
 * * 多线程之间按顺序调用，实现A->B->C
 * * 三个线程启动，要求如下：
 * *  *  *
 * * AA打印5次，BB打印10次，CC打印15次
 * * 接着
 * * AA打印5次，BB打印10次，CC打印15次
 * * ......来10轮
 * *  *
 */
public class ThreadOrderAccess {
    public static void main(String[] args) {
        ShareData shareData = new ShareData();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                shareData.print5();
            }

        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                shareData.print10();
            }

        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                shareData.print15();
            }

        }, "C").start();

    }
}
